package com.yssoft.service.production;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.yssoft.common.Constants;
import com.yssoft.controller.LabelController;
import com.yssoft.entity.core.TDepartment;
import com.yssoft.entity.production.TLabels;
import com.yssoft.mapper.core.TDepartmentMapper;
import com.yssoft.mapper.production.TLabelsMapper;
import com.yssoft.utils.LayuiUtil;
import com.yssoft.utils.MakeEncode;
import com.yssoft.utils.SessionUtil;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @类名称: LabelsService
 * @类描述: 标签管理service
 * @作者: 王永宁
 * @创建时间:2018年5月11日 下午3:07:58
 */
@Service
public class LabelsService {
	private static Logger logger=Logger.getLogger(LabelsService.class);
    @Resource
    private TLabelsMapper labelsMapper;
    
    @Resource
    private TDepartmentMapper deptMapper;
   

    /**
     * @方法名称: save
     * @功能描述: 保存标签
     * @作者: 王永宁
     * @创建时间:2018年5月11日 下午3:09:55
     * @param request
     * @param entity void
     */
	public void save(TLabels entity) {
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); 
		String code="";
		if(entity.getTraceType().contains("1")){
			 code = "LP" + Calendar.getInstance().getTimeInMillis();
		}else{
			 code = "LB" + Calendar.getInstance().getTimeInMillis();
		}
		
		entity.setCode(code);
		TDepartment dept = deptMapper.selectByPrimaryKey(entity.getDeptId());
		entity.setDeptName(dept.getName());
		String ip =request.getServerName();
		String basePath ="http://"+ip+":"+request.getServerPort()+request.getContextPath()+Constants.QRENCODE_SCAN_PATH;
		String url = MakeEncode.QREncode(basePath+code);
		entity.setUrl(url);
		entity.setTurl(url);
		labelsMapper.insertSelective(entity);
		
		
	}

    /**
     * 根据标签申请id查看标签
     *王永宁
     * @param limit 
     * @param page 
     * @return
     */
	public JSONObject selectLabelList(int page, int limit, String labelId) {
		PageHelper.startPage(page,limit);
		List<Map<String, Object>> list = labelsMapper.selectLabelList(Integer.parseInt(labelId));
		int count=labelsMapper.selectLabelListCount(Integer.parseInt(labelId));
		JSONObject jsonObject =LayuiUtil.getJSONObjectByMap(list,count,"","");
		return jsonObject;
		
	}
 
}


